<?php

session_start();
ob_start();
include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$zen_order_id = isset($_POST["zen_order_id"]) ? intval($_POST["zen_order_id"]) : 0;
$by_product_customer = isset($_POST["by_product_customer"]) ? intval($_POST["by_product_customer"]) : 0;
$by_order_status = isset($_POST["by_order_status"]) ? intval($_POST["by_order_status"]) : 0;
$by_province = isset($_POST["by_province"]) ? intval($_POST["by_province"]) : 0;
$by_payment = isset($_POST["by_payment"]) ? intval($_POST["by_payment"]) : 0;
$search_text = isset($_POST["search_text"]) ? addslashes(trim($_POST["search_text"])) : '';
$customer_order_id = isset($_POST["customer_order_id"]) ? intval($_POST["customer_order_id"]) : 0;
$start_time = isset($_POST["start_time"]) ? addslashes(trim($_POST["start_time"])) : '';
$end_time = isset($_POST["end_time"]) ? addslashes(trim($_POST["end_time"])) : '';
if ($start_time != "" && $end_time != "") {
    if (strtotime($end_time) <= strtotime($start_time)) {
        echo json_encode(array('type' => 1, 'msg' => "Bạn phải chọn thời gian kết thúc lớn hơn thời gian bắt đầu!"));
        die;
    }
}
$where = '';
$order_by = '';
if ($by_order_status > 0) {
    switch ($by_order_status) {
        case 1:
            //store
            $where.='order_status=0';
            if ($start_time != "") {
                $where.=' AND store_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND store_date<"' . $end_time . '"';
            }
            $order_by .= "store_date asc";
            break;
        case 2:
            //online
            $where.='order_status=1';
            if ($start_time != "") {
                $where.=' AND online_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND online_date<"' . $end_time . '"';
            }
            $order_by .= "online_date asc";
            break;
        case 3:
            //hàng có sẵn
            $where.='order_status=2';
            break;
        case 4:
            //đã nhận hàng
            $where.='order_status=3';
            if ($start_time != "") {
                $where.=' AND received_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND received_date<"' . $end_time . '"';
            }
            $order_by .= "received_date asc";
            break;
        case 5:
            //đang shipping
            $where.='order_status=4';
            break;
        case 6:
            //khách đã nhận
            $where.='order_status=5';
            if ($start_time != "") {
                $where.=' AND saled_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND saled_date<"' . $end_time . '"';
            }
            $order_by .= "saled_date asc";
            break;
        case 7:
            //hết hàng
            $where.='order_status=6';
            break;
        case 8:
            //ORDER
            $where.='order_status=7';
            if ($start_time != "") {
                $where.=' AND customer_order_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND customer_order_date <"' . $end_time . '"';
            }
            $order_by .= "customer_order_date asc";
            break;
        case 9:
            //chưa chắc chắn
            $where.='order_status=8';
            break;
    }
} else {
    if ($start_time != "") {
        if ($where == '') {
            $where.='(store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        } else {
            $where.=' AND (store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        }
    }
    if ($end_time != "") {
        if ($where == '') {
            $where.='(store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        } else {
            $where.=' AND (store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        }
    }
}
if ($by_payment > 0) {
    switch ($by_payment) {
        case 1:
            //thanh toán hết
            if ($where == '') {
                $where.='total_current_money=0';
            } else {
                $where.=' AND total_current_money=0';
            }
            break;
        case 2:
            //đã cọc
            if ($where == '') {
                $where.='total_payment > 0';
            } else {
                $where.=' AND total_payment > 0';
            }
            break;
        case 3:
            //chưa thanh toán
            if ($where == '') {
                $where.='total_payment = 0';
            } else {
                $where.=' AND total_payment = 0';
            }
            break;
    }
}
if ($search_text !== "") {
    if ($by_product_customer == 1) {
        if ($where == '') {
            $where.="product_code LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND product_code LIKE '%" . $search_text . "%'";
        }
    } elseif ($by_product_customer == 2) {
        if ($where == '') {
            $where.="facebook_name LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND facebook_name LIKE '%" . $search_text . "%'";
        }
    }
}
if ($by_province > 0) {
    switch ($by_province) {
        case 1:
            //tỉnh
            if ($where == '') {
                $where.='province=1';
            } else {
                $where.=' AND province=1';
            }
            break;
        case 2:
            //Hà Nội
            if ($where == '') {
                $where.='province=0';
            } else {
                $where.=' AND province=0';
            }
            break;
    }
}
$zen_customer_order = $core->getZenCustomerOrderById($customer_order_id);
if ($core->checkZenOrderByID($zen_order_id) && $zen_customer_order) {
    $total_result = array();
    $customer_order_detail = $core->getInfoByOrderStatus($zen_order_id, $customer_order_id, $where, $total_result, 0);
    $html_total_result = '';
    if ($customer_order_detail) {
        $html_total_result = '<table class="table table-striped table-bordered bootstrap-datatable datatable">
                        <tr>
                                <th>Tổng số khách hàng</th>
                                <th>Tổng số sản phẩm</th>';
        if ($_SESSION["user_role"] == 1) {
            $html_total_result.='<th>Tổng AT<br/>(EU)</th>
                                 <th>Tổng PT<br/>(EU)</th>';
        }

        $html_total_result.='<th>Tổng tiền hàng<br/>(x1000 VNĐ)</th>
                                <th>Tổng cọc<br/>(x1000 VNĐ)</th>
                                <th>Tổng thừa<br/>(x1000 VNĐ)</th>
                                <th>Tổng thiếu<br/>(x1000 VNĐ)</th>
                               
                            </tr>
                            <tr>
                                <td>' . $total_result["total_customer"] . '</td>
                                <td>' . $total_result["total_product"] . '</td>';
        if ($_SESSION["user_role"] == 1) {
            $html_total_result.='<td>' . number_format($total_result["total_at"]) . '</td>
                                 <td>' . number_format($total_result["total_pt"]) . '</td>';
        }
        $html_total_result.='<td>' . number_format($total_result["total_order_money"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_payment"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_excess_cash"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_debit"] / 1000) . '</td>
                                
                            </tr>
                    </table>';

        //get info of order detail
        $count = 1;
        $array_order_detail_id = array();
        $array_product_price = array();
        foreach ($customer_order_detail as $detail) {
            if ($count == 1) {
                $total_payment =$detail->total_payment;
                $total_price = $detail->total_money;
                if ($detail->total_current_money >= 0) {
                    $total_excess_cash = $detail->total_current_money;
                    $total_debit = 0;
                } else {
                    $total_excess_cash = 0;
                    $total_debit = $detail->total_current_money;
                }
            }
            $array_order_detail_id[]=$detail->id;
            $array_product_price[] = number_format($detail->product_price/1000);
            $count++;
        }
        echo json_encode(array('type' => 0, 'html_total_result' => $html_total_result,
            'total_excess_cash' => number_format($total_excess_cash/1000), 'total_debit' => number_format($total_debit/1000),
            'total_price' => number_format($total_price/1000),'order_detail_id'=>$array_order_detail_id,
            'array_product_price'=>$array_product_price,'total_payment'=>  number_format($total_payment/1000)));
        die;
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Có lỗi xảy ra."));
        die;
    }
} else {
    echo json_encode(array('type' => 1, 'msg' => "Có lỗi xảy ra, vui lòng f5 lại trang."));
    die;
}
?>